<template>
  <div class="jobmore ct">
    <template v-if="showRoot">
      <el-breadcrumb class="head-nav" :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/yardhome', query: {yardId:id} }">站院首页</el-breadcrumb-item>
        <el-breadcrumb-item>站院科普</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="s-content">
        <!-- 公益讲座 -->
        <div class="lecture-section">
          <div class="section-header">
            <div class="header-left">
              <img src="../../assets/resources/lecture-icon.png" alt="">
              <div class="header-title">公益讲座</div>
            </div>
            <div class="header-more" @click="toMore('lecture')">
              >> 查看更多
            </div>
          </div>
          <div class="lecture-cards">
            <div class="lecture-card" v-for="(item, index) in lectureData.slice(0, 5)" :key="index" @click="goDetail(item)">
              <div class="card-image">
                <img :src="item.imageUrl || item.informationImage || '../../assets/resources/default-lecture.jpg'" alt="">
              </div>
              <div class="card-title">
                {{ item.title || item.informationTitle || '暂无标题' }}
              </div>
            </div>
          </div>
        </div>


        <!-- 三农课堂 -->
        <div class="classroom-section">
          <div class="section-header">
            <div class="header-left">
              <img src="../../assets/resources/classroom-icon.png" alt="">
              <div class="header-title">三农课堂</div>
            </div>
            <div class="header-more" @click="toMore('classroom')">
              >> 查看更多
            </div>
          </div>
          <div class="classroom-cards">
            <div class="classroom-card" v-for="(item, index) in classroomData.slice(0, 3)" :key="index" @click="goDetail(item)">
              <div class="card-image">
                <img :src="item.imageUrl || item.informationImage || '../../assets/resources/default-classroom.jpg'" alt="">
              </div>
              <div class="card-title">
                {{ item.title || item.informationTitle || '暂无标题' }}
              </div>
            </div>
          </div>
        </div>


        <!-- 河南农技大讲堂 -->
        <div class="henan-lecture-section">
          <div class="section-header">
            <div class="header-left">
              <img src="../../assets/resources/lecture-icon.png" alt="">
              <div class="header-title">河南农技大讲堂</div>
            </div>
            <div class="header-more" @click="toMore('henan_lecture')">
              >> 查看更多
            </div>
          </div>
          <div class="henan-lecture-cards">
            <div class="henan-lecture-card" v-for="(item, index) in henanLectureData.slice(0, 3)" :key="index" @click="goDetail(item)">
              <div class="card-image">
                <img :src="item.imageUrl || item.informationImage || '../../assets/resources/default-lecture.jpg'" alt="">
              </div>
              <div class="card-title">
                {{ item.title || item.informationTitle || '暂无标题' }}
              </div>
            </div>
          </div>
        </div>
        
      </div>
    </template>
    <router-view v-else></router-view>
  </div>
</template>
<script setup>
import { ArrowRight } from '@element-plus/icons-vue'
import { pageInformation } from '@/api/home';
import { useRouter } from 'vue-router';
import { computed, ref, onMounted, watch } from 'vue'

const router = useRouter();

const id = computed(() => router?.currentRoute?.value?.query.yardId);

// 判断是否显示根页面内容
const showRoot = computed(() => {
  return router?.currentRoute?.value?.name === 'yardkepu'
})

// 公益讲座数据
const lectureData = ref([]);
// 河南农技大讲堂数据
const henanLectureData = ref([]);
// 三农课堂数据
const classroomData = ref([]);

// 获取公益讲座数据
const getLectureData = async() => {
  const params = {
    params: {
      status: true,
      courtyardId: id.value,
      category: 'public_lecture'
    },
    page: {
      pageSize: 5,
      pageNum: 1,
    }
  }
  const ret = await pageInformation(params)
  lectureData.value = ret?.data?.list || [];
}

// 获取河南农技大讲堂数据
const getHenanLectureData = async() => {
  const params = {
    params: {
      status: true,
      courtyardId: id.value,
      category: 'henan_class'
    },
    page: {
      pageSize: 5,
      pageNum: 1,
    }
  }
  const ret = await pageInformation(params)
  henanLectureData.value = ret?.data?.list || [];
}

// 获取三农课堂数据
const getClassroomData = async() => {
  const params = {
    params: {
      status: true,
      courtyardId: id.value,
      category: 'agriculture_class'
    },
    page: {
      pageSize: 3,
      pageNum: 1,
    }
  }
  const ret = await pageInformation(params)
  classroomData.value = ret?.data?.list || [];
}

// 查看更多
const toMore = (type) => {
  if (type === 'lecture') {
    router.push(`/yardkepu/lecture-more?yardId=${id.value}`);
  } else if (type === 'henan_lecture') {
    router.push(`/yardkepu/henan-lecture-more?yardId=${id.value}`);
  } else if (type === 'classroom') {
    router.push(`/yardkepu/classroom-more?yardId=${id.value}`);
  }
}

// 跳转到详情页
const goDetail = (item) => {
  window.open(item.content, '_blank');
  // router.push(`/yardkepuDetail/${item.informationId}?yardId=${id.value}`)
}

// 页面加载时获取数据
onMounted(() => {
  if (showRoot.value) {
    getLectureData();
    getHenanLectureData();
    getClassroomData();
  }
})

// 监听路由变化
watch(() => router.currentRoute.value, (newRoute) => {
  if (newRoute.name === 'yardkepu') {
    getLectureData();
    getHenanLectureData();
    getClassroomData();
  }
}, { immediate: true })
</script>
<style lang="scss" scoped>
.jobmore {
  flex: 1;
  width: 100%;
  padding: 0 10%;
  color: #777;
  background: url('../../assets/xiaoyuan/4.jpeg') no-repeat center center / cover;

  .head-nav {
    display: flex;
    align-items: center;
    font-size: 24px;
    padding: 40px 0 30px;
  }
  
  .s-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 1000px;
    width: 100%;
    font-size: 24px;
  }

  // 通用标题栏样式
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 0;
    margin-bottom: 20px;
    position: relative;
    
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 1px;
      background-color: #e0e0e0;
    }
    
    .header-left {
      display: flex;
      align-items: center;
      img {
        width: 50px;
        height: 50px;
        margin-right: 20px;
        object-fit: contain;
      }
      .header-title {
        font-size: 32px;
        font-weight: bold;
        color: #333;
      }
    }
    .header-more {
      color: #068043;
      font-size: 24px;
      cursor: pointer;
      &:hover {
        text-decoration: underline;
      }
    }
  }

  // 公益讲座样式
  .lecture-section {
    margin-bottom: 60px;
    
    .lecture-cards {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 20px;
      padding-bottom: 10px;
      margin-top: 30px;
      
      .lecture-card {
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        cursor: pointer;
        transition: transform 0.2s;
        height: 420px;
        
        &:hover {
          transform: translateY(-5px);
        }
        
        .card-image {
          height: 350px;
          overflow: hidden;
          
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        
        .card-title {
          font-size: 16px;
          font-weight: bold;
          color: #333;
          padding: 0 15px;
          text-align: center;
          line-height: 1.2;
          height: 60px;
          display: flex;
          align-items: center;
          justify-content: center;
          word-break: break-word;
          overflow: hidden;
          box-sizing: border-box;
        }
      }
    }
  }

  // 河南农技大讲堂样式
  .henan-lecture-section {
    margin-bottom: 60px;
    
    .henan-lecture-cards {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 20px;
      padding-bottom: 10px;
      margin-top: 30px;
      
      .henan-lecture-card {
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        cursor: pointer;
        transition: transform 0.2s;
        height: 420px;
        
        &:hover {
          transform: translateY(-5px);
        }
        
        .card-image {
          height: 350px;
          overflow: hidden;
          
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        
        .card-title {
          font-size: 16px;
          font-weight: bold;
          color: #333;
          padding: 0 15px;
          text-align: center;
          line-height: 1.2;
          height: 60px;
          display: flex;
          align-items: center;
          justify-content: center;
          word-break: break-word;
          overflow: hidden;
          box-sizing: border-box;
        }
      }
    }
  }

  // 三农课堂样式
  .classroom-section {
    .classroom-cards {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 20px;
      padding-bottom: 10px;
      margin-top: 30px;
      
      .classroom-card {
        background: #fff;
        border-radius: 8px;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        cursor: pointer;
        transition: transform 0.2s;
        height: 300px;
        
        &:hover {
          transform: translateY(-5px);
        }
        
        .card-image {
          height: 240px;
          overflow: hidden;
          
          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
        
        .card-title {
          font-size: 16px;
          font-weight: bold;
          color: #333;
          padding: 0 15px;
          text-align: center;
          line-height: 1.2;
          height: 60px;
          display: flex;
          align-items: center;
          justify-content: center;
          word-break: break-word;
          overflow: hidden;
          box-sizing: border-box;
        }
      }
    }
  }
}
</style>